<!--  -->
<template>
  <div class="switching-companies">
        <Dropdown v-if="companyList.length" @on-click="switchingCompany">
            <a href="javascript:void(0)">
                {{selectedCompany}}
                <Icon :size='16' type="ios-arrow-down"></Icon>
            </a>
            <DropdownMenu slot="list">
                <DropdownItem :selected="item.companyId==selectedCompanyId" v-for="item in companyList" :name="item.companyId" :key="item.companyId">{{item.name}}</DropdownItem>
            </DropdownMenu>
        </Dropdown>
  </div>
</template>

<script>
// import { getCompany,getCompanyId ,setCompanyId } from '@/libs/util'

import { mapActions } from "vuex";
export default {
    props:{
        list: {
            type: Array,
            default () {
                return []
            }
        }
    },
  data () {
    return {
        selectedCompany:'暂无绑定公司',
        selectedCompanyId:null,
        companyList:[],
    };
  },

  components: {},

  methods: {
       ...mapActions(["getAuth"]),
      switchingCompany(item){
        setCompanyId(item);
        this.selectedCompanyId = item
        //this.selectedCompany = item.name
        this.getAuth().then(()=>{
            this.$router.go(0)
        });
        //在切换公司时清空本地存储中的页面list，只保留首页
        // let arr = JSON.parse(window.localStorage.getItem("tagNaveList"));
        // console.log(arr);
        // let newArr = [];
        // newArr.push(arr[0]);
        // window.localStorage.setItem('tagNaveList',JSON.stringify(newArr));
       let res = this.list.filter(item => item.name === this.$config.homeName)
       this.$emit('on-close', res, 'all')
      },
      getCompany(){
        this.companyList = JSON.parse(getCompany());
        this.selectedCompanyId = parseInt(getCompanyId());
        this.companyList.forEach((item,index)=>{
            if(item.companyId==this.selectedCompanyId){
                this.selectedCompany = item.name;
                return;
            }
        }) 
      }
  },

  computed: {},

  mounted(){
      this.getCompany();
  },

}

</script>
<style lang='less' scoped>
@import "~@/less/common.less";
    .ivu-dropdown-menu {
        min-width: 100px;
        max-height: 600px;
        overflow-y: auto;
    }
 .switching-companies{
     margin-right: 20px;
     a{
         color: @color-text;
         font-size: 14px;
     }
     .ivu-dropdown-item-selected, .ivu-dropdown-item-selected:hover{
         color: @mainColor;
     }
 }
</style>